package com.itmk.system.role.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itmk.system.role.entity.Role;
import com.itmk.system.role.mapper.RoleMapper;
import com.itmk.system.role.service.RoleService;
import com.itmk.system.user_role.entity.UserRole;
import com.itmk.system.user_role.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper,Role> implements RoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;
//    根据用户Id查找角色集合
    public List<Role> findRoleListByUserIdService(Long userId){
        return this.baseMapper.findRoleListByUserId(userId);
    }

    @Override
    public UserRole getRoleIdByUserId(Long userId) {
        return userRoleMapper.getRoleIdByUserId(userId);
    }

    @Override
    public Boolean assingRole(UserRole userRole) {
        //先删除该用户原来的角色
        QueryWrapper<UserRole> query = new QueryWrapper<>();
        query.lambda().eq(UserRole::getUserId,userRole.getUserId());
        Integer d=userRoleMapper.delete(query);
        Integer i=userRoleMapper.insert(userRole);
        return d*i>0;
    }


}
